import { addEmployessAPI, deleteEmployessAPI, getEmployessListAPI, getUserInfoAPI, importExcelAPI, updateEmployessInfoAPI,assignRolesAPI,getRoleListAPI} from '@/api/employeesApi'

const actions = {
  /* 发送请求获取员工列表的action */
  async fetchEmployessList(context, pageParams) {
    const { data } = await getEmployessListAPI(pageParams)
    context.commit('setEmployessInfo', data)
  },
  /* 发送请求删除员工的action */
  async deleteEmployess(context, id) {
    const { message } = await deleteEmployessAPI(id)
    return message
  },
  /* 发送请求新增员工的action */
  async addEmployess(context, data) {
    const { message } = await addEmployessAPI(data)
    return message
  },
  /* 发送请求批量导入员工的action */
  async importExcel(context, data) {
    const { message } = await importExcelAPI(data)
    return message
  },
  /* 发送请求获取员工基本信息的action */
  async fetchUserInfo(context, id) {
    const { data } = await getUserInfoAPI(id)
    return data
  },
  /* 发送请求保存员工基本信息的action */
  async updateEmployessInfo(context, data) {
    const { message } = await updateEmployessInfoAPI(data)
    return message
  },
  /* 发送请求获取所有角色列表的action */
  async fetchRoleList({ commit }) {
    const { data: role } = await getRoleListAPI()
    return role.rows
  },
  /* 发送请求给员工分配角色的action */
  async assignRoles({ commit }, roleParams) {
    const { message } = await assignRolesAPI(roleParams)
    return message
  },
  /* 根据员工ID发送请求获取员工基本信息的action */
  async findUserDetail({commit}, userId) {
    const {data} = await getUserInfoAPI(userId)
    return data
  },
}
const mutations = {
  /* 设置员工信息的mutation */
  setEmployessInfo(state, employessInfo) {
    state.employessInfo = employessInfo
  }
}
const state = {
  employessInfo: {}
}
const getters = {
  employessList: state => state.employessInfo.rows,
  total: state => state.employessInfo.total
}
export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
